<template>
  <!-- 发现好歌单 -->
  <div class="musicList">
    <!-- 头部 -->
    <div class="musicTop">
      <div class="title">发现好歌单</div>
      <div class="more">查看更多</div>
    </div>
    <!-- 详情 -->
    <div class="musicContent">
      <van-swipe
        :loop="false"
        :width="130"
        class="my-swpie"
        :show-indicators="false"
      >
      <!-- 传入每个歌曲的id -->
        <van-swipe-item v-for="item in musicList" :key="item" @click="tiaozhuan(item.id)">
          <img :src="item.picUrl" alt="网络请求错误" />
          <span class="playCount">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-bofangliang"></use>
            </svg>
            {{ changeCount(item.playCount) }}
          </span>
          <span class="name">{{ item.name }}</span>
        </van-swipe-item>
      </van-swipe>
    </div>
  </div>
</template>
<script>
import { getMusicList } from "@/Api/API.js";
// import { reactive, onMounted } from "vue";
export default {
  //   Vue2
  data() {
    return {
      musicList: [],//歌单数据
    };
  },
  methods: {
    //   获取歌单数据
    async getGnedan() {
      let res = await getMusicList();
      this.musicList = res.data.result;
    },
    //   把播放量的个位转成万或者亿
    changeCount: function (num) {
      if (num >= 100000000) {
        return (num / 100000000).toFixed(1) + "亿";
      } else if (num >= 10000) {
        return (num / 10000).toFixed(1) + "万";
      }
    },
    tiaozhuan:function(id){
          this.$router.push({
            path:"/XiangQing",
            query:{
            id:id
            }
        })
    }
  },
  mounted() {
    this.getGnedan();
  },
  // Vue3
  //   setup() {
  //     const state = reactive({
  //       musicList: [],
  //     });
  //     function changeCount(num) {
  //       if (num >= 100000000) {
  //         return (num / 100000000).toFixed(1) + "亿";
  //       } else if (num >= 10000) {
  //         return (num / 10000).toFixed(1) + "万";
  //       }
  //     }
  //     onMounted(async () => {
  //       let res = await getMusicList();
  //       console.log(res);
  //       state.musicList = res.data.result;
  //     });
  //     return { state, changeCount };
  //   },
};
</script>
<style lang="less" scoped>
.musicList {
  width: 100%;
  height: 5rem;
  padding: 0.2rem;
  .musicTop {
    width: 100%;
    height: 0.6rem;
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.2rem;
    .title {
      font-size: 0.4rem;
      font-weight: 900;
    }
    .more {
      border: 1px solid #ccc;
      text-align: center;
      line-height: 0.6rem;
      padding: 0 0.2rem;
      border-radius: 0.4rem;
    }
  }
  .musicContent {
    width: 100%;
    height: 3.6rem;
    overflow: hidden;
    .van-swipe-item {
      //   margin-right: 0.14rem;
      padding-right: 0.2rem;
      position: relative;
      height: 3.8rem;
      img {
        width: 100%;
        height: 2.4rem;
        border-radius: 0.2rem;
        //   position: absolute;
      }
      .playCount {
        position: absolute;
        z-index: 100;
        right: 0.3rem;
        color: white;
        margin-top: 0.06rem;
        .icon {
          width: 0.3rem;
          height: 0.3rem;
        }
      }
      .name {
        bottom: 0px;
        // 多行省略号
        overflow: hidden;
        display: -webkit-box; //将盒子转换为弹性盒子
        -webkit-line-clamp: 2; // 设置显示多少行
        text-overflow: ellipsis; //文本以省略号显示
        -webkit-box-orient: vertical; //文本显示方式，默认水平
      }
    }
    /deep/ .van-swipe__track {
      display: flex;
    }
  }
}
</style>